﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;


namespace AIC_CD
{
    public static class MyCommon
    {
        public static string GetDonViID(string UserID)
        {

            string sql = "Select DV.ID " +
                         "From tblDonVi DV " +
                         "Inner Join tblUser U On U.DonViID=DV.ID " +
                         "Where U.ID=" + UserID;
            DataTable dtDV = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
            return dtDV.Rows[0]["ID"].ToString();
        }

        public static string GetTenDonVi(string UserID)
        {

            string sql = "Select DV.TenDV " +
                         "From tblDonVi DV " +
                         "Inner Join tblUser U On U.DonViID=DV.ID " +
                         "Where U.ID=" + UserID;
            DataTable dtDV = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
            return dtDV.Rows[0]["TenDV"].ToString();
        }

        public static int GetQuyenID(string MaQuyen)
        {
            string sql = "SELECT * FROM dbo.tblDanhMuc WHERE MaQuyen = '" + MaQuyen + "'";
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql);
            return Convert.ToInt32(dt.Rows[0]["ID"].ToString());
        }

        public static Dictionary<string, int> CheckPermission(int NhomID, int QuyenID)
        {
            Dictionary<string, int> Role = null;
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@NhomID", SqlDbType.Int, ParameterDirection.Input, NhomID));
            arrParams.Add(SqlHelper.CreateParameterObject("@QuyenID", SqlDbType.Int, ParameterDirection.Input, QuyenID));
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, "SELECT * FROM dbo.tblPhanQuyen WHERE UserNhomID_DM = @NhomID AND QuyenID_DM = @QuyenID", arrParams);
            Role = new Dictionary<string, int>();
            if (dt.Rows.Count > 0)
            {
                Role.Add("Xem", Convert.ToInt32(dt.Rows[0]["Xem"].ToString()));
                Role.Add("Them", Convert.ToInt32(dt.Rows[0]["Them"].ToString()));
                Role.Add("Sua", Convert.ToInt32(dt.Rows[0]["Sua"].ToString()));
                Role.Add("Xoa", Convert.ToInt32(dt.Rows[0]["Xoa"].ToString()));
            }
            else
            {
                Role.Add("Xem", 0);
                Role.Add("Them", 0);
                Role.Add("Sua", 0);
                Role.Add("Xoa", 0);
            }
            return Role;
        }

        public static Dictionary<string, int> CheckPermission(string MaQuyen)
        {
            Dictionary<string, int> Role = new Dictionary<string, int>();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            lstParams.Add(SqlHelper.CreateParameterObject("@UserNhomID_DM", SqlDbType.NVarChar, ParameterDirection.Input, HttpContext.Current.Session["quyenuser"]));
            lstParams.Add(SqlHelper.CreateParameterObject("@MaQuyen", SqlDbType.NVarChar, ParameterDirection.Input, MaQuyen));
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text,
            "SELECT PQ.ID,pq.Xem,PQ.Sua FROM dbo.tblPhanQuyen PQ " +
            "INNER JOIN dbo.tblDanhMuc DM ON DM.ID = PQ.QuyenID_DM " +
            "WHERE DM.MaQuyen=@MaQuyen " +
            "AND PQ.UserNhomID_DM = @UserNhomID_DM ", lstParams);
            if (dt.Rows.Count == 0)
            {
                return null;
            }
            else
            {
                Role.Add("Xem", Convert.ToInt32(dt.Rows[0]["Xem"].ToString()));
                Role.Add("Sua", Convert.ToInt32(dt.Rows[0]["Sua"].ToString()));
                return Role;
            }
        }

        public static void AddMessage(MasterPage masterPage, string MessageText, string MessageType)
        {
            if (MessageType == MyConst.MessageType.Info)
            {
                Panel pnl = (Panel)masterPage.FindControl("InfoMessage");
                pnl.Visible = true;
                LiteralControl objPanelText = pnl.Controls[0] as LiteralControl;
                objPanelText.Text = MessageText;
            }
            else if (MessageType == MyConst.MessageType.Success)
            {
                Panel pnl = (Panel)masterPage.FindControl("SuccessMessage");
                pnl.Visible = true;
                LiteralControl objPanelText = pnl.Controls[0] as LiteralControl;
                objPanelText.Text = MessageText;
            }
            else if (MessageType == MyConst.MessageType.Warning)
            {
                Panel pnl = (Panel)masterPage.FindControl("WarningMessage");
                pnl.Visible = true;
                LiteralControl objPanelText = pnl.Controls[0] as LiteralControl;
                objPanelText.Text = MessageText;
            }
            else if (MessageType == MyConst.MessageType.Error)
            {
                Panel pnl = (Panel)masterPage.FindControl("ErrorMessage");
                pnl.Visible = true;
                LiteralControl objPanelText = pnl.Controls[0] as LiteralControl;
                objPanelText.Text = MessageText;
            }
        }

        public static void Download(string sFileName, string sFilePath)
        {
            HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STREAM";
            String Header = "Attachment; Filename=" + sFileName;
            HttpContext.Current.Response.AppendHeader("Content-Disposition", Header);
            System.IO.FileInfo Dfile = new System.IO.FileInfo(HttpContext.Current.Server.MapPath(sFilePath));
            HttpContext.Current.Response.WriteFile(Dfile.FullName);
            HttpContext.Current.Response.End();
        }
    }
}
